<?php
/*
 * 视频类 auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 29605931e0cb861bb073b8b03d1132d13d990e47 $
 */
/**
 * 视频
 * Class AutoBrand
 */
class AutoVideo {

	private static $_table;
	private static $_tableData;
	private static $_pn;
	private static $_pagesize = 16;

	private static function _getTable() {
		if (!empty(self::$_table)) {
			return self::$_table;
		} else {
			self::$_table = Table::name(Table::NAME_VIDEO);
			return self::$_table;
		}
	}

	private static function _getTableData() {
		if (!empty(self::$_tableData)) {
			return self::$_tableData;
		} else {
			self::$_tableData = Table::name(Table::NAME_VIDEO_DATA);
			return self::$_tableData;
		}
	}

	public static function getPn() {
		if (!empty(self::$_pn)) {
			return self::$_pn;
		} else {
			self::$_pn = new Pagination();
			self::$_pn->padding(false);
			self::$_pn->recordsPerPage(self::$_pagesize);
			return self::$_pn;
		}
	}

	/**
	 * 获取品牌相关视频
	 * @param     $brandid
	 * @param int $limit
	 * @return array
	 */
	public static function byBrand($brandid, $limit = 8) {
		global $db;
		$sql = "select itemid,thumb,linkurl from " . self::_getTable() . " where thumb !='' and arbid={$brandid} limit {$limit}";
		return $db->getAll($sql);
	}

	/**
	 * 获取车系相关视频
	 * @param $carid
	 * @param $limit
	 * @return array
	 */
	public static function byCar($carid, $limit = 4) {
		global $db;
		$sql = "select itemid,title,thumb,linkurl from " . self::_getTable() . " where arcid={$carid} limit {$limit}";
		return $db->getAll($sql);
	}

	/**
	 * 获取视频数据
	 * @param $itemid
	 * @return array|null
	 */
	public static function byItemid($itemid) {
		global $db;
		return $db->getOne("SELECT * FROM " . self::_getTable() . " a," . self::_getTableData() . " c WHERE a.itemid=c.itemid and a.itemid=$itemid");
	}

	/**
	 * 获取视频列表
	 * @param string $condition
	 * @param string $order
	 * @param string $cache
	 * @return array
	 */
	public static function getList($condition = 'status=3', $order = 'addtime DESC', $cache = '') {
		global $db;
		$num = $db->count(self::_getTable(), $condition, $cache);
		self::getPn()->records($num);
		$sql = "SELECT * FROM " . self::_getTable();
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . (self::getPn()->getPage() - 1) * self::$_pagesize . ',' . self::$_pagesize;
		$lists  = $catids = $CATS = array();
		$result = $db->query($sql, $cache);
		while ($r = $db->fetchArray($result)) {
			$r['adddate']        = timetodate($r['addtime'], 5);
			$r['editdate']       = timetodate($r['edittime'], 5);
			$r['alt']            = $r['title'];
			$r['title']          = set_style($r['title'], $r['style']);
			$catids[$r['catid']] = $r['catid'];
			$lists[]             = $r;
		}
		return $lists;
	}

	/**
	 * 简单更新点击量
	 * @param $itemid
	 * @return array|bool|mixed|mysqli_result|string
	 */
	public static function updateHits($itemid) {
		global $db;
		$sql = Sql::update(self::_getTable(), 'hits=hits+1', "itemid=$itemid");
		return $db->query($sql);
	}
}